package com.ruoyi.project.system.controller;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.vo.RecruitPersonVo;
import com.ruoyi.project.system.service.ITRecruitPersonService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TRecruitPersonInfo;
import com.ruoyi.project.system.service.ITRecruitPersonInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 义工招募基础简历Controller
 *
 * @author ruoyi
 * @date 2021-10-05
 */
@RestController
@RequestMapping("/person/info")
public class TRecruitPersonInfoController extends BaseController {
    @Autowired
    private ITRecruitPersonInfoService tRecruitPersonInfoService;

    @Autowired
    private ITRecruitPersonService itRecruitPersonService;

    /**
     * 查询义工招募基础简历列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TRecruitPersonInfo tRecruitPersonInfo) {
        startPage();
        List<TRecruitPersonInfo> list = tRecruitPersonInfoService.selectTRecruitPersonInfoList(tRecruitPersonInfo);
        return getDataTable(list);
    }

    /**
     * 导出义工招募基础简历列表
     */
    @Log(title = "义工招募基础简历", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TRecruitPersonInfo tRecruitPersonInfo) {
        List<TRecruitPersonInfo> list = tRecruitPersonInfoService.selectTRecruitPersonInfoList(tRecruitPersonInfo);
        ExcelUtil<TRecruitPersonInfo> util = new ExcelUtil<TRecruitPersonInfo>(TRecruitPersonInfo.class);
        return util.exportExcel(list, "义工招募基础简历数据");
    }

    /**
     * 获取义工招募基础简历详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        TRecruitPersonInfo one = tRecruitPersonInfoService.getOne(new LambdaQueryWrapper<TRecruitPersonInfo>()
                .eq(TRecruitPersonInfo::getPersonId, id));
        RecruitPersonVo personVo = itRecruitPersonService.selectTRecruitPersonById(id);
        int isBlack = personVo.getIsBlack();
        if (one != null) {
            one.setIsBlack(isBlack);
        }
        return AjaxResult.success(one);
    }

    /**
     * 新增义工招募基础简历
     */
    @Log(title = "义工招募基础简历", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TRecruitPersonInfo tRecruitPersonInfo) {

        if (StringUtils.isBlank(tRecruitPersonInfo.getPersonId())) {
            return AjaxResult.error("人员ID不能为空");
        }
        TRecruitPersonInfo one = tRecruitPersonInfoService.getOne(new LambdaQueryWrapper<TRecruitPersonInfo>()
                .eq(TRecruitPersonInfo::getPersonId, tRecruitPersonInfo.getPersonId()));
        RecruitPersonVo personVo = itRecruitPersonService.selectTRecruitPersonById(tRecruitPersonInfo.getPersonId());
        personVo.setIsBlack(tRecruitPersonInfo.getIsBlack());

        itRecruitPersonService.updateTRecruitPerson(personVo);
        if (null != one) {
            tRecruitPersonInfo.setId(one.getId());
            tRecruitPersonInfo.setUpdateTime(new Date());
            tRecruitPersonInfo.setUpdateBy(getUsername());
        }
        tRecruitPersonInfo.setCreateBy(getUsername());
        tRecruitPersonInfo.setCreateTime(new Date());

        return toAjax(tRecruitPersonInfoService.saveOrUpdate(tRecruitPersonInfo));
    }

    /**
     * 修改义工招募基础简历
     */
    @Log(title = "义工招募基础简历", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TRecruitPersonInfo tRecruitPersonInfo) {
        return toAjax(tRecruitPersonInfoService.updateById(tRecruitPersonInfo));
    }

    /**
     * 删除义工招募基础简历
     */
    @Log(title = "义工招募基础简历", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tRecruitPersonInfoService.removeByIds(CollectionUtils.arrayToList(ids)));
    }
}